import type { YmDialogProps } from 'ym-design';

export type TChoiceType = 'org' | 'role' | 'pos';

//当前部门/当前角色/岗位包含的人员
export type TPerson = {
  combineId: string; // 注：当同一个人在不同的部门/角色/岗位时，就无法唯一性，personId+departId/personId+roleId/personId+positionId 才具有唯一性
  personId: string;
  personName: string;
  phone: string;
  hdpic?: string;
};

//--------------部门 orgList 接口数据
// 部门/子级部门详情
export type IOrgItem = {
  combineId: string;
  guid: string; //部门id
  name: string;
  lowerLevel: 0 | 1; // 是否存在子级部门：0否1是
  num: 0; //当前部门下的人员数量(不包含子部门)
};

//当前部门下的人员
export type IOrgPerson = {
  guid: string;
  name: string;
  phone: string; //人员唯一特征
  hdpic?: string;
};

interface IOrgDetail {
  type: 1 | 2; // 1部门 2员工
  partsList?: IOrgItem[] | IOrgPerson[];
}

// 角色岗位

//------------------角色 roleList 接口数据
// 角色分组， 角色是没有层级(tree)结果的
export type TRoleGroupItem = {
  groupId: string;
  isDefault: 0 | 1; // 是否默认 0否 1是
  name: string;
  rlist?: TRole[];
};

// 角色分组下的角色
export type TRole = {
  combineId?: string;
  guid?: string;
  name?: string; //为了与IOrgItem保持一致而添加
  roleId: string;
  roleName: string;
  roleNum: number; //当前角色下的人员
};

//角色：角色分组+当前角色包含的人员
export type IRoleDetail = {
  roleGroupList?: TRoleGroupItem[];
  plist?: TPerson[];
};

//-----------------岗位 positionList 接口数据
//岗位分组
export type TPositionGroupItem = {
  groupId: string;
  isDefault: 0 | 1; // 是否默认 0否 1是
  name: string;
  plist?: TPosition[];
};

// 岗位分组下的岗位
export type TPosition = {
  combineId: string;
  guid: string;
  code: string;
  name: string;
  peopleNumber: number; //当前岗位下的人员
};

export type IPositionDetail = {
  positionGroupList?: TPositionGroupItem[];
  plist?: TPerson[];
};

//---------选择/回显的数据格式----------------
// 部门/角色/岗位

export type TChoiceStaffValue = {
  combineId: string;
  choiceType: TChoiceType;
  guid?: string;
  name?: string;
  personId?: string;
  personName?: string;
  phone?: string;
  hdpic?: string;
};

//------接口参数
export type orgRequestParams = {
  entId: string;
  keyWord?: string;
  departId?: string;
};

export type roleRequestParams = {
  entId: string;
  keyWord?: string;
  roleId?: string;
};

export type posRequestParams = {
  entId: string;
  keyWord?: string;
  positionId?: string;
};

//--------
type TRequestParams = {
  entId: string;
  entName: string;
};

export interface YmChoiceStaffModalProps {
  prefixCls?: string;
  modalPorps?: Partial<Omit<YmDialogProps, 'onOk'>> & {
    onOk?: (e: React.SyntheticEvent, newVal: TChoiceStaffValue[]) => void;
  };
  defaultValue?: TChoiceStaffValue[];
  value?: TChoiceStaffValue[];
  onlyChoicePeople?: boolean;
  choiceBtn?: 'checkbox' | 'radio';
  onChange?: (newVal: TChoiceStaffValue[]) => void;
  orgRequest?: (params: orgRequestParams) => Promise<IOrgDetail[] | []>;
  roleRequest?: (params: roleRequestParams) => Promise<IRoleDetail | undefined>;
  posRequest?: (
    params: posRequestParams,
  ) => Promise<IPositionDetail | undefined>;
  requestParams: TRequestParams;
  tabNames?: string[];
}

export type CrumbItem = {
  guid: string;
  name: string;
  entId?: string;
};
export interface CrumbListProps {
  prefixCls: string;
  crumbList: CrumbItem[];
  onCrumbClick: (currCrumb: CrumbItem) => void;
}

export interface ChoiceListRef {
  search: (keyword: string | undefined) => void;
  delete: (selectedItem: TChoiceStaffValue) => void;
  resetSelectedValues: (newSelValues: TChoiceStaffValue[]) => void;
}

type ChoiceChangeFn = (
  operationType: 'add' | 'delete',
  operationItem: TChoiceStaffValue,
) => void;

type ChoiceChangeBatchFn = (
  operationType: 'add' | 'delete',
  operationList: TChoiceStaffValue[],
) => void;
export interface OrgChoiceListProps {
  prefixCls: string;
  keyWord: string | undefined;
  defaultValue?: TChoiceStaffValue[];
  onlyChoicePeople: boolean;
  choiceBtn: 'checkbox' | 'radio';
  onOrgChoiceChange: ChoiceChangeFn;
  onOrgChoiceChangeBatch: ChoiceChangeBatchFn;
  requestParams: TRequestParams;
  orgRequest: (params: orgRequestParams) => Promise<IOrgDetail[] | []>;
}

export interface RoleChoiceListProps {
  prefixCls: string;
  keyWord: string | undefined;
  defaultValue?: TChoiceStaffValue[];
  onlyChoicePeople: boolean;
  choiceBtn: 'checkbox' | 'radio';
  onRoleChoiceChange: ChoiceChangeFn;
  onRoleChoiceChangeBatch: ChoiceChangeBatchFn;
  requestParams: TRequestParams;
  roleRequest: (params: roleRequestParams) => Promise<IRoleDetail | undefined>;
}

export interface PosChoiceListProps {
  prefixCls: string;
  keyWord: string | undefined;
  defaultValue?: TChoiceStaffValue[];
  onlyChoicePeople: boolean;
  choiceBtn: 'checkbox' | 'radio';
  onPosChoiceChange: ChoiceChangeFn;
  onPosChoiceChangeBatch: ChoiceChangeBatchFn;
  requestParams: TRequestParams;
  posRequest: (
    params: posRequestParams,
  ) => Promise<IPositionDetail | undefined>;
}
